package com.ruoyi.project.warehouse.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.warehouse.domain.RyDepotRecordAuditVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.warehouse.domain.RyDepotRecordAudit;
import com.ruoyi.project.warehouse.service.IRyDepotRecordAuditService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 出入库审核Controller
 *
 * @author ruoyi
 * @date 2023-02-17
 */
@RestController
@RequestMapping("/warehouse/audit")
public class RyDepotRecordAuditController extends BaseController
{
    @Autowired
    private IRyDepotRecordAuditService ryDepotRecordAuditService;

    /**
     * 查询出入库审核列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:audit:list')")
    @GetMapping("/list")
    public TableDataInfo list(RyDepotRecordAudit ryDepotRecordAudit)
    {
        startPage();
        List<RyDepotRecordAuditVo> list = ryDepotRecordAuditService.selectRyDepotRecordAuditList(ryDepotRecordAudit);
        return getDataTable(list);
    }

    /**
     * 导出出入库审核列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:audit:export')")
    @Log(title = "出入库审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RyDepotRecordAudit ryDepotRecordAudit)
    {
        List<RyDepotRecordAuditVo> list = ryDepotRecordAuditService.selectRyDepotRecordAuditList(ryDepotRecordAudit);
        ExcelUtil<RyDepotRecordAuditVo> util = new ExcelUtil<RyDepotRecordAuditVo>(RyDepotRecordAuditVo.class);
        util.exportExcel(response, list, "出入库审核数据");
    }

    /**
     * 获取出入库审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:audit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ryDepotRecordAuditService.selectRyDepotRecordAuditById(id));
    }

    /**
     * 删除出入库审核
     */
    @PreAuthorize("@ss.hasPermi('warehouse:audit:remove')")
    @Log(title = "出入库审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ryDepotRecordAuditService.deleteRyDepotRecordAuditByIds(ids));
    }

    /**
     * 审核出入库修改信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:audit:audit')")
    @Log(title = "出入库审核", businessType = BusinessType.UPDATE)
    @PutMapping("/{ids}")
    public AjaxResult audit(@PathVariable Long[] ids)
    {
        int result = 0;
        result = ryDepotRecordAuditService.auditRyDepotRecord(ids);
        if(result == -1){
            return AjaxResult.error("当前库存不足");
        }
        return toAjax(result);
    }


    /**
     * 作废出入库修改信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:audit:cancel')")
    @Log(title = "出入库修改作废", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult cancel(@RequestBody RyDepotRecordAuditVo ryDepotRecordAuditVo)
    {
        RyDepotRecordAudit ryDepotRecordAudit =new RyDepotRecordAudit();
        ryDepotRecordAudit.setAuditStatus(2);
        ryDepotRecordAudit.setId(ryDepotRecordAuditVo.getId());
        return toAjax(ryDepotRecordAuditService.updateRyDepotRecordAudit(ryDepotRecordAudit));
    }
}
